package com.shamar.blog.util;

import com.shamar.blog.dto.ImageHolder;
import net.coobird.thumbnailator.Thumbnails;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class ImageUtil {
	public static String generateThumbnail(InputStream thumbnailInputStream, String fileName, String targetAddr) {
		String realFileName = FileUtil.getRandomFileName();
		String extension = getFileExtension(fileName);
		makeDirPath(targetAddr);
		String relativeAddr = targetAddr + realFileName + extension;
		File dest = new File(FileUtil.getImgBasePath() + relativeAddr);
		try {
//			Thumbnails.of(thumbnail.getInputStream()).size(200, 200).outputQuality(0.25f).toFile(dest);
			Thumbnails.of(thumbnailInputStream).size(230, 230).outputQuality(1.0f).toFile(dest);
		} catch (IOException e) {
			throw new RuntimeException("创建缩略图失败：" + e.toString());
		}
		return relativeAddr;
	}

	public static String generateThumbnails(ImageHolder thumbnail, String targetAddr) {
		//获取不重复的随机名
		String realFileName = FileUtil.getRandomFileName();
		//获取文件扩展名如png等
		String extension = getFileExtension(thumbnail.getImageName());
		//如果目标路径不存在,则自动创建
		makeDirPath(targetAddr);
//		获取文件存储的相对路径(带文件名)
		String relativeAddr = targetAddr + realFileName + extension;
		//获取文件要保存到的目标路径
		File dest = new File(FileUtil.getImgBasePath() + relativeAddr);
		try {
//			Thumbnails.of(thumbnail.getInputStream()).size(200, 200).outputQuality(0.25f).toFile(dest);
			Thumbnails.of(thumbnail.getImage()).size(200, 200).outputQuality(0.25f).toFile(dest);
		} catch (IOException e) {
			throw new RuntimeException("创建缩略图失败：" + e.toString());
		}
		return relativeAddr;
	}

	public static String generateNormalImg(ImageHolder thumbnail, String targetAddr) {
		String realFileName = FileUtil.getRandomFileName();
		String extension = getFileExtension(thumbnail.getImageName());
		makeDirPath(targetAddr);
		String relativeAddr = targetAddr + realFileName + extension;
		File dest = new File(FileUtil.getImgBasePath() + relativeAddr);
		try {
			Thumbnails.of(thumbnail.getImage()).size(337, 640).outputQuality(1.0f).toFile(dest);
		} catch (IOException e) {
			throw new RuntimeException("创建图片失败：" + e.toString());
		}
		return relativeAddr;
	}

//	public static List<String> generateNormalImgs(List<CommonsMultipartFile> imgs, String targetAddr) {
//		int count = 0;
//		List<String> relativeAddrList = new ArrayList<String>();
//		if (imgs != null && imgs.size() > 0) {
//			makeDirPath(targetAddr);
//			for (CommonsMultipartFile img : imgs) {
//				String realFileName = FileUtil.getRandomFileName();
//				String extension = getFileExtension(img);
//				String relativeAddr = targetAddr + realFileName + count + extension;
//				File dest = new File(FileUtil.getImgBasePath() + relativeAddr);
//				count++;
//				try {
//					Thumbnails.of(img.getInputStream()).size(600, 300).outputQuality(0.5f).toFile(dest);
//				} catch (IOException e) {
//					throw new RuntimeException("创建图片失败：" + e.toString());
//				}
//				relativeAddrList.add(relativeAddr);
//			}
//		}
//		return relativeAddrList;
//	}

	/**
	 * 创建目标路径所涉及的目录,如/user/fxm/hh/ll则 user fxm hh三个文件夹都将自动创建
	 * @param targetAddr
	 */
	private static void makeDirPath(String targetAddr) {
		String realFileParentPath = FileUtil.getImgBasePath() + targetAddr;
		File dirPath = new File(realFileParentPath);
		if (!dirPath.exists()) {
			dirPath.mkdirs();
		}
	}

	private static String getFileExtension(String fileName) {
//		String originalFileName = cFile.getOriginalFilename();
//		return originalFileName.substring(originalFileName.lastIndexOf("."));
		return fileName.substring(fileName.lastIndexOf("."));
	}
}
